/*
 * @Author: anshengtyp
 * @Date: 2024-03-27 16:54:01
 * @LastEditTime: 2024-03-28 09:13:47
 * @LastEditors: anshengtyp
 * @Description: 请求
 */
import axios from 'axios'
import { getToken } from '@/utils/auth'
import { tansParams } from '@/utils/index'

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

// 创建axios实例
const service = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  // 超时
  timeout: 100000
})

// 请求拦截
service.interceptors.request.use(config => {
  if (getToken()) {
    config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
  if (config.method === 'get' && config.params) {
    let url = config.url + '?' + tansParams(config.params);
    url = url.slice(0, -1);
    config.params = {};
    config.url = url;
  }
  return config
}, error => {
  Promise.reject(error)
})

service.interceptors.response.use(res => {
  return res.data
})

export default service


